import passwordDialog from '../passwordDialog.html';
import './list.less';

class ListController {
  /**
   *
   * @param $scope
   */
  constructor($scope, c2cAdminSvc, $uibModal, $location, $cookies) {
    "ngInject";

    this.$scope = $scope;
    this.service = c2cAdminSvc;
    this.$uibModal = $uibModal;
    this.$location = $location;
    this.$cookies = $cookies;

    this.VipOne = {};
    this.VipTwo = {};
    this.VipThree = {};
    this.list = [];
    this.init();
  }

  init() {
    this.getConfig();
  }

  //查询配置
  getConfig() {
    this.list = [];
    this.service.userMemberConfigInfo().then(data => {
      if (data.code == window.CODE.SUCCESS) {
        for (let i of data.data) {
          if (i.configType == 'VIP1') {
            this.VipOne = i;
          } else if (i.configType == 'VIP2') {
            this.VipTwo = i;
          } else if (i.configType == 'VIP3') {
            this.VipThree = i;
          } else if (i.configType == 'ASSET_CODE_FEE') {
            this.list.push(i);
          }
        }
      } else {
        window.toast(data.msg)
      }
    })
  }

  //更新某项配置
  updata(type, name, val, code, index, order) {

    if (typeof name == 'object') {
      for (let i of name) {
        if (!this[type][i]) {
          window.toast('请输入更改的值');
          return false;
        }
      }
    } else {
      if (code) {
        if (!this.list[index][name]) {
          window.toast('请输入更改的值');
          return false;
        }
      } else {
        if (!this[type][name]) {
          window.toast('请输入更改的值');
          return false;
        }
      }

    }

    let param = {
      id: code ? this.list[index].id : this[type].id,
      type: code ? code : val
    }

    if (val == 'ensureAsset') {
      param.ensureAsset = this[type][name];
    } else if (val == 'adNum') {
      param.buyAdNum = this[type].buyAdNum;
      param.sellAdNum = this[type].sellAdNum;
    } else if (val == 'adAssetNum') {
      param.buyCoinNum = this[type].buyCoinNum;
      param.sellCoinNum = this[type].sellCoinNum;
    }

    if (code) {
      param.assetCode = this.list[index].assetCode;
      param.assetCodeFee = this.list[index][name];
      param.feeType = order;
    }


    let that = this;

    let modalInstance = this.$uibModal.open({
      template: passwordDialog,
      scope: this.$scope,
      controller() {}
    });

    this.$scope.$$ok = function (password) {
      if (password === '') {
        window.toast('请输入当前账号密码');
        return;
      }
      let header = {
        'login-password': password
      };
      that.service.updataUserMemberConfigInfo(param, header).then(data => {
        if (data.code == window.CODE.SUCCESS) {
          that.getConfig();
        } else {
          window.toast(data.msg)
        }
      })
      modalInstance.dismiss('ok');
    };

    this.$scope.$$cancel = function () {
      modalInstance.dismiss('cancel');
    };

  }

}


export default ListController;